package domain.worm;

import sounds.Sounds;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.events.constants.Key;
import com.uqbar.vainilla.sound.Sound;

import domain.components.PowerBar;
import domain.components.SightComponent;


public class PointingState extends WormState {

	private SightComponent sight;
	private double potencia = 0;
	private double limiteDePotencia;
	private double movX, movY;
	private Boolean seDisparo = false;
	private PowerBar pwb;
	private Worm worm;
	
	public PointingState(Worm worm) {
		super(worm);
		
		if(this.getWorm().getDir() == 1)
			this.getWorm().setAppearance(this.getWorm().getWeapon().getAppWeaponRight());
		else
			this.getWorm().setAppearance(this.getWorm().getWeapon().getAppWeaponLeft());
		
		this.movX = 0;
		this.movY = -1;
		this.sight = new SightComponent(this.getWorm().getX()/*+ this.getWorm().getDiameter()/2*/,
										this.getWorm().getY(), this.getWorm());
		this.pwb = new PowerBar(worm.getX()- worm.getDiameter()/2, worm.getY()+ worm.getDiameter()+5);
		this.getWorm().getScene().addComponent(this.sight);
		this.worm = worm;
		
		(Sounds.sounds.get(worm.getWeapon().getKeyPathSoundReload())).play();
		
	}

	public void update(DeltaState deltaState) {		

		if(deltaState.isKeyPressed(Key.SPACE) && !seDisparo){
			this.limiteDePotencia = this.getWorm().getLimitPowerWeapon();
			this.getWorm().getWeapon().setSight(this.sight);
			//creo la bala con la posicion x e y de el componente sight
			this.seDisparo = true;
		}
		if(seDisparo){
			this.potencia = this.potencia + 400 * deltaState.getDelta();
			if(this.limiteDePotencia != 0){
				int c = (int)this.potencia/100;
				this.pwb.setearApariencia(c);
				this.getWorm().getScene().addComponent(this.pwb);
			}
			if(deltaState.isKeyReleased(Key.SPACE) || this.potencia >= this.limiteDePotencia){
				this.getWorm().getWeapon().shoot(this.getWorm().getDir(),this.sight.getAngulo(), this.getWorm().getDir()*this.potencia, this.getWorm().getScene());
				this.potencia = 0;
				seDisparo = false;
				this.pwb.destroy();
				(Sounds.sounds.get(worm.getWeapon().getKeyPathSoundFire())).play();
				this.toStandBy();
				}
			
		}


		if (deltaState.isKeyPressed(Key.RIGHT) | deltaState.isKeyPressed(Key.LEFT)){
			this.toStandBy();
		}
	}
	
	

	public void toStandBy(){
		super.toStandBy();
		this.sight.destroy();
	}
	public void toFallingState(){
		super.toFallingState();
		this.sight.destroy();
	}
}
